package com.qst.rongxiaoserver.config;

import com.qst.rongxiaoserver.common.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

// Web配置类   配置静态资源访问路径   配置拦截器   配置Swagger的信息   生成API文档
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${web.upload.path}")
    private String uploadPath;

    @Autowired
    JwtInterceptor jwtInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        // 配置静态资源访问路径
        registry.addResourceHandler("/uploads/**")
               .addResourceLocations("file:" + uploadPath);

        // 配置knife4j的静态资源访问
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        // 添加JWT拦截器
//        registry.addInterceptor(jwtInterceptor)
//                // 拦截所有请求
//                .addPathPatterns("/**")
//                // 排除不需要验证的路径
//                .excludePathPatterns(
//                        "/api/address/*",  // 地址列表接口
//                        "/admin/login",  // 登录接口
//                        "/doc.html",     // swagger文档
//                        "/webjars/**",   // swagger静态资源
//                        "/uploads/**",    // 上传文件
//                        "/v3/api-docs/**", // swagger API文档
//                        "/swagger-ui/*"
//                );
    }
}